package com.tony.demo.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.puhui.lib.base.LoadingDialog;
import com.puhui.lib.base.mvp.IView;
import com.puhui.lib.utils.DMLog;
import com.puhui.lib.utils.MyToastUtil;
import com.tony.demo.mvp.presenter.interfaces.IBasePresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

/**
 * Copyright:
 * Created by TangJian on 2019/4/20.
 * Description:
 * Modified:
 */

@SuppressWarnings("unused")
public abstract class BaseFragment<P extends IBasePresenter> extends Fragment implements IView {

    protected P presenter;
    protected View mView;
    private LoadingDialog loadingDialog;

    public abstract P createPresenter();

    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = createPresenter();
        if (presenter != null) {
            presenter.attachView(this);
        }
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(getViewLayout(), container, false);
        initView();
        initData();
        return mView;
    }

    protected void initData() {
    }

    protected void initView() {
    }

    protected abstract int getViewLayout();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        stopLoading();

        if (presenter != null) {
            presenter.detachView();
        }

        EventBus.getDefault().unregister(this);
    }

    @Override
    public void startActivity(@NotNull Class<?> clazz, boolean checkLogin) {

    }

    @Override
    public void startActivityForResult(@NotNull Class<?> clazz, int requestCode) {

    }

    /**
     * 如果不写一个方法，那么运行时会把错说当前类或父类中没有使用Subscribe注解的方法
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void doBaseEvent(String temp) {
        DMLog.e(this.getClass().getSimpleName(), "temp");
    }

    protected void startActivity(Class clazz) {
        startActivity(new Intent(getContext(), clazz));
    }

    @Override
    public void startLoading(String text) {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog.getInstance(getContext());
        }
        loadingDialog.show(text);
    }

    @Override
    public void stopLoading() {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog.getInstance(getContext());
        }
        loadingDialog.dismiss();
    }

    @Override
    public void showToast(String toastContent) {
        MyToastUtil.getInstance().show(toastContent);
    }

    @Override
    public void showToast(int resourceId) {
        MyToastUtil.getInstance().show(resourceId);
    }
}
